//
//  HomeController.m
//  LiquorChain
//
//  Created by 王冬 on 2018/12/20.
//  Copyright © 2018年 王冬. All rights reserved.
//

#import "HomeController.h"
#import "LiquorlistController.h"
#import "AbilityListController.h"
#import "HomeOneCell.h"
@interface HomeController ()<SDCycleScrollViewDelegate,UITableViewDelegate,UITableViewDataSource,FloatingBallHeaderDelegate>
@property (nonatomic, strong) FloatingBallHeader *floatingBallHeader;
// 轮播图
@property (nonatomic,strong) SDCycleScrollView *cycleScrollView;
@property (nonatomic,strong) SDCycleScrollView *cycleScrollText;
// 横向滚动新闻控件
@property (nonatomic,strong) LMJScrollTextView * scrollTextView;
// 横向滚动新闻
@property (nonatomic,strong) UITextField * speedTF;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *listArray;
// tableview 头部view
@property (nonatomic,strong) UIView *headView;
// 加速酿酒按钮
@property (nonatomic,strong) UIButton *quickenBtn;

// 圣酒
@property (nonatomic,strong) UIButton *liquorBtn;
// 圣酒酿力
@property (nonatomic,strong)UIButton *abilityBtn;
@end

@implementation HomeController

static NSString *CellIdentifier = @"Cell";
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setTabeleView];
    [self creatHeadView];

}
#pragma mark  创建TableView
-(void)setTabeleView{
    self.tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, -kNavStausHeight, SCREEN_WIDTH, SCREEN_HEIGHT - kNavStausHeight - kIPhoneXBottomHeight +kNavStausHeight) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.sectionFooterHeight = 0.01;
    [self.tableView registerNib:[UINib nibWithNibName:@"HomeOneCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
    [self.view addSubview:self.tableView];
}
#pragma mark 创建tableview头部视图
-(void)creatHeadView{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 950)];
    self.headView = headView;
   
    headView.backgroundColor = RGBA(16.0, 0.0, 52.0, 1);
    
    // 收集酿力
    self.floatingBallHeader = [[FloatingBallHeader alloc] initWithFrame:CGRectMake(0,kNavStausHeight,SCREEN_WIDTH , FloatingBallHeaderHeight)];
    self.floatingBallHeader.delegate = self;
    self.floatingBallHeader.dataList = @[@"1.2", @"0.05", @"1.88", @"10.55", @"20",@"1.2"];
    // , @"33", @"0.01", @"1.23",@"0.4",@"34"
    [headView addSubview:self.floatingBallHeader];
    //加速酿酒
    self.quickenBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    _quickenBtn.frame = CGRectMake(SCREEN_WIDTH - 61, 60, 45, 45);
    _quickenBtn.backgroundColor =[UIColor yellowColor];
    [_quickenBtn addTarget:self action:@selector(quickClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.floatingBallHeader addSubview:_quickenBtn];
   
    // 向左，连续滚动
    [self addLabelWithFrame:CGRectMake(0, 0, 50, 30) text:@"公告:"];
    _scrollTextView = [[LMJScrollTextView alloc] initWithFrame:CGRectMake(50, 0, SCREEN_WIDTH - 50, 30) textScrollModel:LMJTextScrollContinuous direction:LMJTextScrollMoveLeft];
    _scrollTextView.backgroundColor = RGBA(253.0, 140.0, 7.0, 1);
    [self.floatingBallHeader addSubview:_scrollTextView];
    [_scrollTextView startScrollWithText:@"最新公告:联想首发骁龙855新机没能刷屏,小米Play是“罪魁祸首" textColor:[UIColor whiteColor] font:[UIFont systemFontOfSize:12]];
    
    
    // 圣酒 动态 view
    UIView *topView = [[UIViewCor alloc]initWithFrame:CGRectMake(15, 350, headView.width-30, 108) corNumer:10];
    // 圣酒和酿力
    self.liquorBtn =[[UIButton alloc]init];
    [topView addSubview:self.liquorBtn];
    [self.liquorBtn setTitleColor: RGBA(255.0, 228.0, 134.0, 1) forState:UIControlStateNormal];
    //self.liquorBtn.textColor = RGBA(255.0, 228.0, 134.0, 1);
    [self.liquorBtn setTitle:@"0.12345" forState:UIControlStateNormal];
    self.liquorBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.liquorBtn addTarget:self action:@selector(liquorClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.liquorBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(59);
        make.top.mas_equalTo(22);
        make.height.mas_equalTo(13);
    }];
    UILabel *showLiquorLabel =[[UILabel alloc]init];
    [topView addSubview:showLiquorLabel];
    showLiquorLabel.text = @"圣酒";
    showLiquorLabel.textColor =RGBA(255.0, 228.0, 134.0, 1);
    showLiquorLabel.font =[UIFont systemFontOfSize:12];
    [showLiquorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(72);
        make.top.mas_equalTo(42);
        make.height.mas_equalTo(12);
    }];
    
    self.abilityBtn =[[UIButton alloc]init];
    [topView addSubview:self.abilityBtn];
    [self.abilityBtn setTitleColor: RGBA(255.0, 228.0, 134.0, 1) forState:UIControlStateNormal];
    [self.abilityBtn setTitle:@"123" forState:UIControlStateNormal];
    self.abilityBtn.titleLabel.font = [UIFont systemFontOfSize:16];
     [self.abilityBtn addTarget:self action:@selector(abilityClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.abilityBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-80);
        make.top.mas_equalTo(20);
        make.height.mas_equalTo(13);
        
    }];
    UILabel *showAbilityLabel =[[UILabel alloc]init];
    [topView addSubview:showAbilityLabel];
    showAbilityLabel.text = @"酿力";
    showAbilityLabel.textColor =RGBA(255.0, 228.0, 134.0, 1);
    showAbilityLabel.font =[UIFont systemFontOfSize:12];
    [showAbilityLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-80);
        make.top.mas_equalTo(42);
        make.height.mas_equalTo(12);
    }];
    
    topView.backgroundColor = RGBA(110, 10, 48, 1);
    UIView *downView =[[UIViewCor alloc]initWithFrame:CGRectMake(7, 75, topView.width -14, 25) corNumer:13];
    UILabel *leftLabel =[[UILabel alloc]initWithFrame:CGRectMake(17, 0, 30, 24)];
    leftLabel.textColor = RGBA(232.0, 2.0, 87.0, 1);
    leftLabel.text = @"动态";
    leftLabel.font =[UIFont systemFontOfSize:12];
    [downView addSubview:leftLabel];
    
    
    NSArray *titleArray = @[@"纯文字上下滚动轮播纯文字上下滚动轮播纯文字上下滚动轮播纯文字上下滚动轮播纯文字上下滚动轮播",@"纯文字上下滚动轮播1纯文字上下滚动轮播纯文字上下滚动轮播纯文字上下滚动轮播",@"纯文字上下滚动轮播2"];
    
    CGRect rect4 = CGRectMake(30+17, 0, downView.width -34-30, 25);
    self.cycleScrollText = [SDCycleScrollView
                                           cycleScrollViewWithFrame:rect4
                                           delegate:self
                                           placeholderImage:nil];
    _cycleScrollText.titleLabelTextColor = [UIColor whiteColor];
    _cycleScrollText.titleLabelBackgroundColor =  RGBA(95.0, 9.0, 42.0, 1);
    _cycleScrollText.titleLabelTextFont = [UIFont systemFontOfSize:12];
    _cycleScrollText.scrollDirection = UICollectionViewScrollDirectionVertical;
    _cycleScrollText.onlyDisplayText = YES;
    
    _cycleScrollText.titlesGroup = [titleArray copy];
    
    [downView addSubview:_cycleScrollText];
  
    
    downView.backgroundColor = RGBA(95.0, 9.0, 42.0, 1);
    [topView addSubview:downView];
    [headView addSubview:topView];
     UIView *sdcycleView = [[UIViewCor alloc]initWithFrame:CGRectMake(15,topView.y+topView.height+20 , headView.width-30, 100) corNumer:10.0];
    
    NSArray *imageNames = @[@"h1",
                            @"h2",
                            @"h1",
                            @"h2"
                            ];
    self.cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, sdcycleView.width, 100) shouldInfiniteLoop:YES imageNamesGroup:imageNames];
    _cycleScrollView.delegate = self;
    _cycleScrollView.pageControlDotSize = CGSizeMake(10, 10);
    _cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleClassic;
    _cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    _cycleScrollView.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _cycleScrollView.currentPageDotColor = [UIColor blackColor];
    _cycleScrollView.pageDotColor =[UIColor whiteColor];
    [sdcycleView addSubview:_cycleScrollView];
    [headView addSubview:sdcycleView];
    
    UIView *firstView =[[UIViewCor alloc]initWithFrame:CGRectMake(15, sdcycleView.y+sdcycleView.height +20, headView.width -30, 100) corNumer:10];
    
    UILabel *baseLabel =[[UILabel alloc]initWithFrame:CGRectMake(21, 22, 87, 21)];
    baseLabel.text = @"酿酒基地";
    baseLabel.textColor =RGBA(252.0, 202.0, 139.0, 1);
    UILabel *warnLabel =[[UILabel alloc]initWithFrame:CGRectMake(23, 50, 206, 13)];
    warnLabel.text = @"全国第一个酒类区块链落地应用项目";
    warnLabel.font =[UIFont systemFontOfSize:11];
    warnLabel.textColor = RGBA(217.0, 106.0, 50.0, 1);
    UIImageView *rightView =[[UIImageView alloc]initWithFrame:CGRectMake(firstView.width-94, 18, 87, 67)];
    rightView.backgroundColor =[UIColor greenColor];
    [firstView addSubview:rightView];
    [firstView addSubview:warnLabel];
    [firstView addSubview:baseLabel];
    
    firstView.backgroundColor = RGBA(110, 10, 48, 1);
    [headView addSubview:firstView];
    
    UIView *secondView =[[UIViewCor alloc]initWithFrame:CGRectMake(15, firstView.y+firstView.height +16, headView.width -30, 100) corNumer:10];
    
    UILabel *secondbaseLabel =[[UILabel alloc]initWithFrame:CGRectMake(21, 22, 87, 21)];
    secondbaseLabel.text = @"全民品酒";
    secondbaseLabel.textColor =RGBA(252.0, 202.0, 139.0, 1);
    UILabel *secondwarnLabel =[[UILabel alloc]initWithFrame:CGRectMake(23, 50, 206, 13)];
    secondwarnLabel.text = @"全国第一个酒类区块链落地应用项目";
    secondwarnLabel.font =[UIFont systemFontOfSize:11];
    secondwarnLabel.textColor = RGBA(217.0, 106.0, 50.0, 1);
    UIImageView *secondrightView =[[UIImageView alloc]initWithFrame:CGRectMake(secondView.width-94, 18, 87, 67)];
    secondrightView.backgroundColor =[UIColor blueColor];
    [secondView addSubview:secondrightView];
    [secondView addSubview:secondwarnLabel];
    [secondView addSubview:secondbaseLabel];

    secondView.backgroundColor = RGBA(110, 10, 48, 1);
    [headView addSubview:secondView];
    
    UIView *threeView =[[UIViewCor alloc]initWithFrame:CGRectMake(15, secondView.y+secondView.height +16, headView.width -30, 100) corNumer:10];
    
    UILabel *threebaseLabel =[[UILabel alloc]initWithFrame:CGRectMake(21, 22, 87, 21)];
    threebaseLabel.text = @"邀请好友";
    threebaseLabel.textColor =RGBA(252.0, 202.0, 139.0, 1);
    UILabel *threewarnLabel =[[UILabel alloc]initWithFrame:CGRectMake(23, 50, 206, 13)];
    threewarnLabel.text = @"全国第一个酒类区块链落地应用项目";
    threewarnLabel.font =[UIFont systemFontOfSize:11];
    threewarnLabel.textColor = RGBA(217.0, 106.0, 50.0, 1);
    UIImageView *threerightView =[[UIImageView alloc]initWithFrame:CGRectMake(threeView.width-94, 18, 87, 67)];
    threerightView.backgroundColor =[UIColor redColor];
    [threeView addSubview:threerightView];
    [threeView addSubview:threewarnLabel];
    [threeView addSubview:threebaseLabel];
    
    
    threeView.backgroundColor = RGBA(110, 10, 48, 1);
    [headView addSubview:threeView];
    
    
    self.tableView.tableHeaderView = headView;
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    self.navigationController.navigationBar.hidden = YES;

}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    //[self setStatusBarBackgroundColor:[UIColor whiteColor]];
}
#pragma mark 加速酿酒
-(void)quickClick:(UIButton *)sender{
    NSLog(@"%@",@"加速酿酒");
}

//设置状态栏背景颜色
- (void)setStatusBarBackgroundColor:(UIColor *)color {
    
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = color;
    }
}

// 无限滚动
-(void)changeSpeed{
    if([_speedTF.text floatValue]) {
        CGFloat speed = [_speedTF.text floatValue];
        [_scrollTextView setMoveSpeed:speed];
    }
}
// 添加公告
-(void)addLabelWithFrame:(CGRect)frame text:(NSString *)text{
    UILabel * label = [[UILabel alloc] initWithFrame:frame];
    label.backgroundColor =[UIColor colorWithRed:253.0/255.0 green:140.0/255.0 blue:7.0/255.0 alpha:1];;
    label.text      = text;
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.font      = [UIFont boldSystemFontOfSize:12];
    [self.floatingBallHeader addSubview:label];
}
#pragma mark  UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView  *headerView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
    headerView.backgroundColor = RGBA(112.0, 4.0, 9.0, 1);
    UILabel *titleLabel =[[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, 50)];
    titleLabel.text = @"排行榜";
    titleLabel.textColor = RGBA(252.0, 202.0, 139.0, 1);
    titleLabel.font =[UIFont systemFontOfSize:18];
    
    [headerView addSubview:titleLabel];
    
    NSArray *titleArray = @[@"排行榜1小时更新一次",@"两天内不收取圣酒讲暂停酿酒"];
    
    CGRect rect4 = CGRectMake(headerView.width - 200, 10, 185, 30);
    SDCycleScrollView *cycleScrollText = [SDCycleScrollView
                                          cycleScrollViewWithFrame:rect4
                                          delegate:self
                                          placeholderImage:nil];
    cycleScrollText.titleLabelTextAlignment = NSTextAlignmentRight;
    cycleScrollText.titleLabelTextColor = RGBA(252.0, 202.0, 139.0, 1);
    cycleScrollText.titleLabelBackgroundColor =  RGBA(112.0, 4.0, 9.0, 1);
    
    cycleScrollText.titleLabelTextFont = [UIFont systemFontOfSize:11];
    cycleScrollText.scrollDirection = UICollectionViewScrollDirectionVertical;
    cycleScrollText.onlyDisplayText = YES;
    
    cycleScrollText.titlesGroup = [titleArray copy];
    
    [headerView addSubview:cycleScrollText];
//    UILabel *scrLabel =[[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-150, 0, 140, 50)];
//    scrLabel.text = @"显示滚动的内容";
//    scrLabel.textAlignment = NSTextAlignmentCenter;
//    scrLabel.font =[UIFont systemFontOfSize:11];
//    scrLabel.textColor = RGBA(252.0, 202.0, 139.0, 1);
//    [headerView addSubview:scrLabel];
    
    
    UIView *bodyView =[[UIView alloc]initWithFrame:CGRectMake(15, 60, SCREEN_WIDTH-30, 40)];
    bodyView.backgroundColor = RGBA(160.0, 23.0, 74.0, 1);
    [headerView addSubview:bodyView];
    UILabel *leftLabel =[[UILabel alloc]initWithFrame:CGRectMake(13, 0, 98, 40)];
    leftLabel.text = @"酿酒师";
    leftLabel.font =[UIFont systemFontOfSize:14];
    // leftLabel.textAlignment = NSTextAlignmentCenter;
    leftLabel.textColor = RGBA(190.0, 6.0, 67.0, 1);
    [bodyView addSubview:leftLabel];
    
    UILabel *centerLabel =[[UILabel alloc]initWithFrame:CGRectMake(bodyView.width/2-49 , 0 ,98, 40)];
    centerLabel.text = @"圣酒量";
    centerLabel.textAlignment = NSTextAlignmentCenter;
    centerLabel.font =[UIFont systemFontOfSize:14];
    centerLabel.textColor = RGBA(190.0, 6.0, 67.0, 1);
    [bodyView addSubview:centerLabel];
    
    
    UILabel *rightLabel =[[UILabel alloc]initWithFrame:CGRectMake(bodyView.width -13-98 , 0, 98, 40)];
    rightLabel.text = @"酿力值";
    rightLabel.textAlignment = NSTextAlignmentRight;
    rightLabel.font =[UIFont systemFontOfSize:14];
    rightLabel.textColor = RGBA(190.0, 6.0, 67.0, 1);
    [bodyView addSubview:rightLabel];
    
    
    return headerView;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 101;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 20;
   
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HomeOneCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = BackColor;
    cell.leftImageView.hidden = YES;
    cell.leftLabel.hidden = YES;
    if (indexPath.row == 0) {
       
        cell.leftImageView.hidden = NO;
        cell.leftImageView.image =[UIImage imageNamed:@"jin"];
    }else if (indexPath.row == 1) {
        
         cell.leftImageView.hidden = NO;
         cell.leftImageView.image =[UIImage imageNamed:@"yin"];
    }else if (indexPath.row == 2) {
         cell.leftImageView.hidden = NO;
         cell.leftImageView.image =[UIImage imageNamed:@"tong"];
    }
    if (indexPath.row >2) {
        cell.leftImageView.hidden = YES;
        cell.leftLabel.hidden = NO;
       // cell.textLabel.textColor =[UIColor whiteColor];
       // cell.textLabel.font = [UIFont systemFontOfSize:11];
        cell.leftLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row +1];
    }
    
 
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%ld",indexPath.row);
}

// 隐藏UITableViewStyleGrouped下边多余的间隔
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
#pragma mark - 收集酿力代理
#pragma mark FloatingBallHeaderDelegate
- (void)floatingBallHeader:(FloatingBallHeader *)floatingBallHeader didPappaoAtIndex:(NSInteger)index isLastOne:(BOOL)isLastOne {
    NSLog(@"%@",floatingBallHeader.dataList[index]);
    if (isLastOne) {
        // 点了最后一个，刷新
        self.floatingBallHeader.dataList = @[@"2.2", @"0.15", @"8.88", @"5.55", @"2.22", @"3.33", @"0.11", @"1.23", @"6.66", @"7.89"];
    }
    
}
#pragma mark - 圣酒记录 和酿力点击事件
-(void)liquorClick:(UIButton *)sender{
    NSLog(@"圣酒");
   
    
    LiquorlistController *listVC =[[LiquorlistController alloc]init];
    self.navigationController.navigationBar.hidden = NO;
    listVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:listVC animated:YES];
    
}
-(void)abilityClick:(UIButton *)sender{
    AbilityListController *abVC =[[AbilityListController alloc]init];
    self.navigationController.navigationBar.hidden = NO;
     abVC.hidesBottomBarWhenPushed = YES;
   [self.navigationController pushViewController:abVC animated:YES];
}

#pragma mark - 轮播代理
#pragma mark SDCycleScrollViewDelegate

// 点击图片代理方法
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
   
    if (cycleScrollView == _cycleScrollText) {
        NSLog(@"点击的文字滚动");
    }
    
   
    
    // 清理缓存
    [SDCycleScrollView clearImagesCache];
    
}

// 滚动到第几张图片的回调
-(void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index
{
    if (_cycleScrollView == cycleScrollView) {
        // NSLog(@">>>>>> 滚动到第%ld张图", (long)index);
    }
   
}

@end

